/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panels;

import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Alejandro Naranjo
 */
public class VistaModificarNombre extends javax.swing.JDialog 
{
    private String nombre;
    private JFrame parent;
    
    /**
     * Creates new form VistaModificarNombre
     */
    public VistaModificarNombre( java.awt.Frame parent, boolean modal, String nombreAntiguo )
    {
        super(parent, modal);
        this.parent = (JFrame) parent;
        this.nombre = nombreAntiguo;
        initComponents();
        nombreMalla( nombreAntiguo );
        super.setLocationRelativeTo(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonCancelar = new javax.swing.JButton();
        botonAceptar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nuevoNombre = new javax.swing.JTextField();
        nombreAnterior = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(450, 200));
        setMinimumSize(new java.awt.Dimension(450, 200));

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre: ");

        jLabel2.setText("Nuevo Nombre: ");

        nuevoNombre.setText("Ingresa Nuevo Nombre.");
        nuevoNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nuevoNombreKeyPressed(evt);
            }
        });

        nombreAnterior.setText("jLabel3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(187, Short.MAX_VALUE)
                        .addComponent(botonAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nuevoNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nombreAnterior)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreAnterior))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nuevoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonAceptar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botonAceptarActionPerformed
    {//GEN-HEADEREND:event_botonAceptarActionPerformed
        if( this.validarDato() )
        {
            super.setVisible(false);
        }
    }//GEN-LAST:event_botonAceptarActionPerformed
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        super.setVisible(false);
        this.nuevoNombre.setText( nombreAnterior.getText() );
    }      
    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botonCancelarActionPerformed
    {//GEN-HEADEREND:event_botonCancelarActionPerformed
        this.dispose();
        this.nuevoNombre.setText( nombreAnterior.getText() );
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void nuevoNombreKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nuevoNombreKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER){
            this.botonAceptarActionPerformed(null);
        }
    }//GEN-LAST:event_nuevoNombreKeyPressed

    private void nombreMalla( String nombre )
    {
        this.nombreAnterior.setText(nombre);
    }
    
    public String getNuevoNombre()
    {
        return nombre;
    }
    
    private boolean validarDato()
    {
        String nombre = nuevoNombre.getText().trim();
        boolean valido = true;
        nombre = nombre.toLowerCase();
        if( nombre.contains("ingresa nuevo nombre") || nombre.contains("ingresa nuevo") || nombre.contains("nuevo nombre") )
        {
            JOptionPane.showMessageDialog(this.parent, "Error\nNo has modificado el campo de nuevo nombre."
                    + "\nEste campo no debe contener las palabras\n'ingresa nuevo' o 'nuevo nombre'", "Modificar Nombre", JOptionPane.ERROR_MESSAGE);
            valido = false;
        }
        if( nombre.equals("") )
        {
            JOptionPane.showMessageDialog(this.parent, "Error\nCampo de nuevo nombre se encuentra vacío.", "Modificar Nombre", JOptionPane.ERROR_MESSAGE);
            valido = false;
        }
        if( valido )
            this.nombre = this.nuevoNombre.getText();
        else
            this.nombre = this.nombreAnterior.getText();
        return valido;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(VistaModificarNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(VistaModificarNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(VistaModificarNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(VistaModificarNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel nombreAnterior;
    private javax.swing.JTextField nuevoNombre;
    // End of variables declaration//GEN-END:variables
}

